package com.whh.tq.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.whh.tq.bean.QueryResultInfo;

public class ResContentHelper {

	//余票查询返回内容
	//addRow(0,"1,	序号
	//D3106(福州南->上海虹桥)^skbcx.jsp?cxlx=cc&date=20111023&trainCode=D3106,		车次信息，^前面是显示文字，^后面是链接
	//杭州南^skbcx.jsp?cxlx=czjgcc&zm=&date=20111023&stationName_passTrain=%E6%9D%AD%E5%B7%9E%E5%8D%97,		发站信息，^前面是显示文字，^后面是链接
	//上海虹桥^skbcx.jsp?cxlx=czjgcc&zm=&date=20111023&stationName_passTrain=//%E4%B8%8A%E6%B5%B7%E8%99%B9%E6%A1%A5,	到站信息，^前面是显示文字，^后面是链接
	//21:35,23:03,01:28,	发时,到时,用时
	//--,--,--,123,--,--,--,--,--,--,动车",0);		座位信息：商务座，特等座，一等座，二等座，高级软卧，软卧，硬卧，软座，硬座，无座，等级
	
	//时刻表发到站查询返回内容
	//addRow(23,"24,	序号
	//D5481^skbcx.jsp?cxlx=cc&date=20111023&trainCode=D5481,	车次信息,^前面是显示文字，^后面是链接
	//杭州南^skbcx.jsp?cxlx=czjgcc&zm=&date=20111023&stationName_passTrain=%E6%9D%AD%E5%B7%9E%E5%8D%97^self,	发站，^前面是显示文字，^后面是链接 
	//台州^skbcx.jsp?cxlx=czjgcc&zm=&date=20111023&stationName_passTrain=%E5%8F%B0%E5%B7%9E^self,		到站，^前面是显示文字，^后面是链接
	//18:37,21:09,02:32,	发时，到时，用时
	//六安^skbcx.jsp?cxlx=czjgcc&zm=&date=20111023&stationName_passTrain=%E5%85%AD%E5%AE%89^self,		始发站，^前面是显示文字，^后面是链接
	//温州南^skbcx.jsp?cxlx=czjgcc&zm=&date=20111023&stationName_passTrain=%E6%B8%A9%E5%B7%9E%E5%8D%97^self,		终点站，^前面是显示文字，^后面是链接
	//动车,有,--,--,104,87,--,--,--,--,--",23);		等级、空调，商务座，特等座，一等座，二等座，高级软卧上/下，软卧上/下，硬卧上/中/下，软座，硬座

	//时刻表车次查询返回内容
	//addRow(0,"1,	序号
	//昆明^skbcx.jsp?cxlx=czjgcc&zm=&date=20111030&stationName_passTrain=%E6%98%86%E6%98%8E ,		车站
	//K80     , ---- , 16:33 , --,		车次、到时、发时、
	//--,--,--,--,--,--,--,--,--",0);	商务座，特等座，一等座，二等座，高级软卧上/下，软卧上/下，硬卧上/中/下，软座，硬座
	
	//时刻表站点查询返回内容
	//<row id ='0'>
	//<cell>1</cell>	序号
	//<cell><![CDATA[ D5466^skbcx.jsp?cxlx=cc&date=20111102&trainCode=D5466]]></cell>	车次
	//<cell>动车</cell>		等级
	//<cell><![CDATA[ 温州南^skbcx.jsp?cxlx=czjgcc&zm=&date=20111102&stationName_passTrain=%E6%B8%A9%E5%B7%9E%E5%8D%97]]></cell><cell>07:03</cell>		始发站，始发时间
	//<cell><![CDATA[ 合肥^skbcx.jsp?cxlx=czjgcc&zm=&date=20111102&stationName_passTrain=%E5%90%88%E8%82%A5]]></cell><cell>15:53</cell>		终到站，终到时间
	//<cell><![CDATA[ 台州^skbcx.jsp?cxlx=czjgcc&zm=&date=20111102&stationName_passTrain=%E5%8F%B0%E5%B7%9E]]></cell><cell>07:57</cell><cell>07:55</cell><cell>00:52</cell>		车站，发时，到时，历时
	//</row>
	
	private final static String leftTicketRegex = "addRow\\(\\d+?,\"\\d+?,"
			+ "(\\w+?)\\(([\\u4e00-\\u9fa5]+?)->([\\u4e00-\\u9fa5]+?)\\)\\^skbcx.jsp\\?cxlx=cc&date=\\d{8}&trainCode=\\w+?," 
			+ "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)," 
			+ "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?),"
			+ "(\\d{2}:\\d{2}),(\\d{2}:\\d{2}),(\\d{2}:\\d{2}),"
			+ "([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),(([\\u4e00-\\u9fa5]+?)|([-]+?)),([\\u4e00-\\u9fa5]+?)\",\\d+?\\)"; 
	
	private static Pattern leftTicketPattern = Pattern.compile(leftTicketRegex);

	private final static String fromToRegex ="addRow\\(\\d+?,\"\\d+?,"
			+ "(\\w+?)\\^skbcx.jsp\\?cxlx=cc&date=\\d{8}&trainCode=\\w+?,"
			+ "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\^self,\\s*" 
			+ "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\^self,\\s*"
			+ "(\\d{2}:\\d{2}),(\\d{2}:\\d{2}),(\\d{2}:\\d{2}),"
			+ "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\^self,\\s*" 
			+ "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\^self,\\s*"
			+ "([\\u4e00-\\u9fa5]+?),([\\u4e00-\\u9fa5]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-/]+?),([0-9-/]+?),([0-9-/]+?),([0-9-]+?),([0-9-]+?)\",\\d+?\\)"; 
	
	private static Pattern fromToPattern = Pattern.compile(fromToRegex);
	
	private final static String trainCodeRegex = "addRow\\(\\d+?,\"\\d+?,"
			 + "([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\s*,\\s*"
			 + "(\\w+?)\\s*,\\s*((\\d{2}:\\d{2})|([-]+?))\\s*,\\s*((\\d{2}:\\d{2})|([-]+?))\\s*,\\s*((\\d{2}:\\d{2})|([-]+?))\\s*,"
			 + "([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-]+?),([0-9-/]+?),([0-9-/]+?),([0-9-/]+?),([0-9-]+?),([0-9-]+?)\",\\d+?\\)"; 
	
	private static Pattern trainCodePattern = Pattern.compile(trainCodeRegex);
	
	private final static String stationRegex = "<row id ='\\d+?'>"
			+"<cell>\\d+?</cell>"
			+"<cell><\\!\\[CDATA\\[\\s*(\\w+?)\\^skbcx.jsp\\?cxlx=cc&date=\\d{8}&trainCode=\\w+?\\]\\]></cell>"
			+"<cell>([\\u4e00-\\u9fa5]+?)</cell>"
			+"<cell><\\!\\[CDATA\\[\\s*([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\]\\]></cell><cell>(\\d{2}:\\d{2})</cell>"
			+"<cell><\\!\\[CDATA\\[\\s*([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\]\\]></cell><cell>(\\d{2}:\\d{2})</cell>"
			+"<cell><\\!\\[CDATA\\[\\s*([\\u4e00-\\u9fa5]+?)\\^skbcx.jsp\\?cxlx=czjgcc&zm=&date=\\d{8}&stationName_passTrain=((%[A-Z0-9]{2}){3,}?)\\]\\]></cell><cell>(\\d{2}:\\d{2})</cell><cell>(\\d{2}:\\d{2})</cell><cell>(\\d{2}:\\d{2})</cell>"
			+"</row>";

	private static Pattern stationPattern = Pattern.compile(stationRegex);
	
	private static Pattern ictPattern = Pattern.compile("parent\\.document\\.getElementById\\(\"([A-Za-z]{4})\"\\).value=\"([0-9]{0,4})\"");
	
	/**
	 * 解析余票查询返回的内容
	 * @param s
	 * @return
	 */
	public static List<QueryResultInfo> parseLeftTicketResContent(String s) {
		List<QueryResultInfo> res = new ArrayList<QueryResultInfo>();
		if (null == s || s.trim().equals("")) {
			return res;
		}
		Matcher matcher = leftTicketPattern.matcher(s);
		while (matcher.find()) {
			String trainCode = matcher.group(1);
			String startStation = matcher.group(2);
			String endStation = matcher.group(3);
			String fromStation = matcher.group(4);
			String toStation = matcher.group(7);
			String fromTime = matcher.group(10);
			String toTime = matcher.group(11);
			String usedTime = matcher.group(12);
			String bussinessSeat = matcher.group(13);
			String specialSeat = matcher.group(14);
			String firstClassSeat = matcher.group(15);
			String secondClassSeat = matcher.group(16);
			String premiumSoftCouchette = matcher.group(17);
			String softCouchette = matcher.group(18);
			String hardCouchette = matcher.group(19);
			String softSeat = matcher.group(20);
			String hardSeat = matcher.group(21);
			String noSeat = matcher.group(22);
			String level = matcher.group(25);

			QueryResultInfo info = new QueryResultInfo();
			info.setStartStation(startStation);
			info.setEndStation(endStation);
			info.setTrainCode(trainCode);
			info.setFromStation(fromStation);
			info.setToStation(toStation);
			info.setFromTime(fromTime);
			info.setToTime(toTime);
			info.setUsedTime(usedTime);
			info.setBussinessSeat(bussinessSeat);
			info.setSpecialSeat(specialSeat);
			info.setFirstClassSeat(firstClassSeat);
			info.setSecondClassSeat(secondClassSeat);
			info.setPremiumSoftCouchette(premiumSoftCouchette);
			info.setSoftCouchette(softCouchette);
			info.setHardCouchette(hardCouchette);
			info.setSoftSeat(softSeat);
			info.setHardSeat(hardSeat);
			info.setNoSeat(noSeat);
			info.setLevel(level);
			res.add(info);
		}
		return res;
	}
	
	/**
	 * 解析时刻表发到站返回的内容
	 * @param s
	 * @return
	 */
	public static List<QueryResultInfo> parseFromToResContent(String s) {
		List<QueryResultInfo> res = new ArrayList<QueryResultInfo>();
		if (null == s || s.trim().equals("")) {
			return res;
		}
		Matcher matcher = fromToPattern.matcher(s);
		while (matcher.find()) {
			String trainCode = matcher.group(1);// 车次
			String fromStation = matcher.group(2);// 发站
			String toStation = matcher.group(5);// 到站
			String fromTime = matcher.group(8);// 发时
			String toTime = matcher.group(9);// 到时
			String usedTime = matcher.group(10);// 用时
			String startStation = matcher.group(11);// 起始站
			String endStation = matcher.group(14);// 终点站
			String level = matcher.group(17);// 等级
			String bussinessSeat = matcher.group(19);// 商务座
			String specialSeat = matcher.group(20);// 特等座
			String firstClassSeat = matcher.group(21);// 一等座
			String secondClassSeat = matcher.group(22);// 二等座
			String premiumSoftCouchette = matcher.group(23);// 高级软卧
			String softCouchette = matcher.group(24);// 软卧
			String hardCouchette = matcher.group(25);// 硬卧
			String softSeat = matcher.group(26);// 软座
			String hardSeat = matcher.group(27);// 硬座
			

			QueryResultInfo info = new QueryResultInfo();
			info.setStartStation(startStation);
			info.setEndStation(endStation);
			info.setTrainCode(trainCode);
			info.setFromStation(fromStation);
			info.setToStation(toStation);
			info.setFromTime(fromTime);
			info.setToTime(toTime);
			info.setUsedTime(usedTime);
			info.setBussinessSeat(bussinessSeat);
			info.setSpecialSeat(specialSeat);
			info.setFirstClassSeat(firstClassSeat);
			info.setSecondClassSeat(secondClassSeat);
			info.setPremiumSoftCouchette(premiumSoftCouchette);
			info.setSoftCouchette(softCouchette);
			info.setHardCouchette(hardCouchette);
			info.setSoftSeat(softSeat);
			info.setHardSeat(hardSeat);
			info.setLevel(level);
			res.add(info);
		}
		return res;
	}
	
	/**
	 * 解析车次查询返回的内容
	 * @param s
	 * @return
	 */
	public static List<QueryResultInfo> parseTrainCodeResContent(String s) {
		List<QueryResultInfo> res = new ArrayList<QueryResultInfo>();
		if (null == s || s.trim().equals("")) {
			return res;
		} 
		Matcher matcher = trainCodePattern.matcher(s);
		while (matcher.find()) {
			String station = matcher.group(1);//车站
			String fromTime = matcher.group(5);//发时
			String toTime = matcher.group(8);//到时
			String usedTime = matcher.group(11);//用时
			String bussinessSeat = matcher.group(14);//商务座
			String specialSeat = matcher.group(15);//特等座
			String firstClassSeat = matcher.group(16);//一等座
			String secondClassSeat = matcher.group(17);//二等座
			String premiumSoftCouchette = matcher.group(18);//高级软卧
			String softCouchette = matcher.group(19);//软卧
			String hardCouchette = matcher.group(20);//硬卧
			String softSeat = matcher.group(21);//软座
			String hardSeat = matcher.group(22);//硬座
			
			QueryResultInfo info = new QueryResultInfo();
			info.setStation(station);
			info.setFromTime(fromTime);
			info.setToTime(toTime);
			info.setUsedTime(usedTime);
			info.setBussinessSeat(bussinessSeat);
			info.setSpecialSeat(specialSeat);
			info.setFirstClassSeat(firstClassSeat);
			info.setSecondClassSeat(secondClassSeat);
			info.setPremiumSoftCouchette(premiumSoftCouchette);
			info.setSoftCouchette(softCouchette);
			info.setHardCouchette(hardCouchette);
			info.setSoftSeat(softSeat);
			info.setHardSeat(hardSeat);
			
			res.add(info);
		}
		return res;
	}
	
	/**
	 * 解析车次查询返回的内容
	 * @param s
	 * @return
	 */
	public static List<QueryResultInfo> parseStationResContent(String s) {
		List<QueryResultInfo> res = new ArrayList<QueryResultInfo>();
		if (null == s || s.trim().equals("")) {
			return res;
		} 
		Matcher matcher = stationPattern.matcher(s);
		while (matcher.find()) {
			String trainCode = matcher.group(1);
			String level = matcher.group(2);
			String startStation = matcher.group(3);
			String startTime = matcher.group(6);
			String endStation = matcher.group(7);
			String endTime = matcher.group(10);
			String station = matcher.group(11);
			String fromTime = matcher.group(14);
			String toTime = matcher.group(15);
			String usedTime = matcher.group(16);
			
			QueryResultInfo info = new QueryResultInfo();
			info.setTrainCode(trainCode);
			info.setLevel(level);
			info.setStartStation(startStation);
			info.setStartTime(startTime);
			info.setEndStation(endStation);
			info.setEndTime(endTime);
			info.setStation(station);
			info.setFromTime(fromTime);
			info.setToTime(toTime);
			info.setUsedTime(usedTime);
			
			res.add(info);
		}
		return res;
	}
	
	/**
	 * 解析返回内容中的ict参数和ict值
	 * @param html
	 * @return
	 */
	public static Map<String,String> parseIctContent(String html){
		Map<String,String> map = new HashMap<String, String>();
		String ict = "";
		String ictValue = "";
		Matcher m = ictPattern.matcher(html);
		while(m.find()){
			ict = m.group(1);
			ictValue = m.group(2);
		}
		map.put("ict", ict);
		map.put("ictValue", ictValue);
		return map;
	}
	
	/**
	 * 判断是否是中文
	 * @param ch
	 * @return
	 */
	public static boolean isChineseStr(String ch) {
		Pattern p = Pattern.compile("^[\\u4e00-\\u9fa5]+$");
		Matcher m = p.matcher(ch.trim());
		return m.find();
	}
}
